﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Messaging;
using IkeCode.WP7.HQReader.Base;
using IkeCode.WP7.HQReader.Interfaces;
using IkeCode.WP7.Toolkit.Base;
using System.Windows.Navigation;
using IkeCode.WP7.Toolkit.Persist;
using IkeCode.WP7.HQReader.Model;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight.Ioc;

namespace IkeCode.WP7.HQReader.ViewModel
{
    /// <summary>
    /// View Model for Feed Displayer
    /// </summary>
    public class FeedViewModel : BaseViewModel
    {
        #region ## Services ##

        private readonly IFeedDataService _feedDataService;
        private readonly IFeedPublisherDataService _publisherDataService;

        #endregion ## Services ##

        #region ## Properties ##

        private Guid _publisherId;
        /// <summary>
        /// Publisher Guid for Home Tile navigation
        /// </summary>
        public Guid PublisherId
        {
            get { return _publisherId; }
            set
            {
                _publisherId = value;
                RaisePropertyChanged("PublisherId");
                this.BindFeedByTile();
            }
        }

        /// <summary>
        /// Self Uri
        /// </summary>
        public override Uri MyUri
        {
            get
            {
                return new Uri("/Views/FeedPage.xaml", UriKind.RelativeOrAbsolute);
            }
        }

        private List<IIcFeed> _listFeed = new List<IIcFeed>();
        /// <summary>
        /// List os Feeds to be displayed
        /// </summary>
        public List<IIcFeed> ListFeed
        {
            get { return _listFeed; }
            set
            {
                if (_listFeed == value)
                {
                    return;
                }

                _listFeed = value;
                RaisePropertyChanged("ListFeed");
            }
        }

        #endregion ## Properties ##

        /// <summary>
        /// Constructor with Service Interfaces
        /// </summary>
        /// <param name="feedDataService">FeedDataService</param>
        /// <param name="publisherDataService">PublisherDataService</param>
        public FeedViewModel(IFeedDataService feedDataService, IFeedPublisherDataService publisherDataService)
        {
            _feedDataService = feedDataService;
            _publisherDataService = publisherDataService;

            this.BindDesignData();

            Messenger.Default.Register<IIcFeedPublisher>(this, "FeedList", (publisher) =>
            {
                this.PageTitle = publisher.Title;
                this.ListFeed = new List<IIcFeed>();

                _feedDataService.GetFeeds(true, publisher, (feeds, ex) =>
                    {
                        if (ex != null)
                        {
                            _navigationService.GoBack();
                            return;
                        }
                        else
                        {
                            this.ListFeed = feeds;
                        }
                    });
            });
        }

        #region ## Commands ##

        /// <summary>
        /// Command to send to Feed Item Details Page
        /// </summary>
        public ICommand CommandToFeed
        {
            get
            {
                return new RelayCommand((param) =>
                {
                    var obj = (IIcFeed)param;
                    if (obj != null)
                    {
                        //Messenger.Default.Send<IIcFeedPublisher>(obj, "FeedList");
                        //_navigationService.NavigateTo(ViewModelLocator.FeedPageUri);
                    }
                });
            }
        }

        #endregion ## Commands ##

        #region ## Private Methods ##

        private void BindDesignData()
        {
            if (this.IsInDesignMode)
            {
                _feedDataService.GetFeeds(false, null, (feeds, ex) =>
                {
                    this.PageTitle = "Publisher Name";
                    this.ListFeed = feeds;
                });
            }
        }

        private void BindFeedByTile()
        {
            if (this.PublisherId != Guid.Empty)
            {
                _publisherDataService.GetPublishers((publishers, error) =>
                {
                    if (error != null)
                    {
                        MessageBox.Show("Error occurred.");
                    }
                    else
                    {
                        var publisher = publishers.FirstOrDefault(i => i.Id.Equals(this.PublisherId));
                        if (publisher != null)
                        {
                            this.PageTitle = publisher.Title;

                            _feedDataService.GetFeeds(true, publisher, (feeds, ex) =>
                            {
                                if (ex != null)
                                {
                                    this.NavigateTo<MainViewModel>();
                                }
                                else
                                {
                                    this.ListFeed = feeds;
                                }
                            });
                        }
                    }
                });
            }
            else
            {
                this.NavigateTo<MainViewModel>();
            }
        }

        #endregion ## Private Methods ##
    }
}